<?php

function get_picture_list($con, $paradise_id = NULL, $limit = NULL, $keyword = NULL)
{
	$query = "SELECT * FROM picture";
	
	if ($paradise_id != NULL || $keyword != NULL) {
		$query .= " WHERE";
		
		if ($paradise_id != NULL && $keyword != NULL) {
			$query .= " paradise_id = '$paradise_id' AND title LIKE '%$keyword%'";
		}
		else if ($paradise_id != NULL) {
			$query .= " paradise_id = '$paradise_id'";
		}
		else if ($keyword != NULL) {
			$query .= " title LIKE '%$keyword%'";
		}
	}
	
	$query .= " ORDER BY id DESC";
	
	if ($limit !== FALSE) {
		$query .= " LIMIT $limit";
	}
	
	return select($con, $query);
}

function get_picture($con, $id)
{
	$query = "SELECT * FROM picture WHERE id = '$id'";
	$records = select($con, $query);
	if(count($records) == 1) {
		return $records[0];
	}
	else {
		return NULL;
	}
}

function insert_picture($con, $picture)
{
	$title			= $picture["title"];
	$description	= $picture["description"];
	$paradise_id	= $picture["paradise_id"];
	$user_id		= $picture["user_id"];
	$url			= $picture["url"];
	$thumb_big		= $picture["thumb_big"];
	$thumb_small	= $picture["thumb_small"];
	
	$query = "INSERT INTO picture(title, description, paradise_id, user_id, url, thumb_big, thumb_small) " .
		"VALUES ('$title', '$description', '$paradise_id', '$user_id', '$url', '$thumb_big', '$thumb_small')";
	
	return insert($con, $query);
}

function update_picture($con, $picture)
{
	$id				= $picture["id"];
	$title			= $picture["title"];
	$description	= $picture["description"];
	$paradise_id	= $picture["paradise_id"];
	$user_id		= $picture["usre_id"];
	$url			= $picture["url"];
	$thumb_big		= $picture["thumb_big"];
	$thumb_small	= $picture["thumb_small"];	
		
	$query = "UPDATE multimedia SET title = '$title', description = '$description', " .
		"paradise_id = '$paradise_id', user_id = '$user_id', " .
		"url = '$url', thumb_big = '$thumb_big', thumb_small = '$thumb_small' " .
		"WHERE id = '$id'";
	
	return update($con, $query);
}